<?php
class AppFile extends CActiveRecord
{
     public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

     public function tableName() {                  
            return 'c_app_file';
        }
        
        
     public function getAppFileByExt($appId,$extension){
        $sql = "SELECT id,appId,filename,extension,refcode_path,create_date FROM c_app_file 
        WHERE appId = ".intval($appId)." AND extension='".$extension."'";

        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();
        return $rows;
    }
    
     public function getAllAppFileByExt($appId,$extension){
        $sql = "SELECT id,appId,filename,extension,refcode_path,create_date FROM c_app_file 
        WHERE appId = ".intval($appId)." AND extension='".$extension."'";

        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        return $rows;
    }
    
    
    public function getAppFile($appId, $os)
    {
        $condition = "";
        if(!empty($os)){
            $condition .= $os=="j2me" ? " AND extension='jar'" : "";
            $condition .= $os=="android" ? " AND extension='apk'" : "";
            $condition .= $os=="ios" ? " AND extension='plist'" : "";
        }
        
        $sql = "SELECT id, filename, extension, filesize,introtext,refcode_path,create_date FROM c_app_file WHERE appId=" . $appId . "  AND status=1" . $condition;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        
        return $rows;
    }
    
      public function getAppFileByExtension($appId, $extension)
    {
        $condition ="";
        if(!empty($extension))
        $condition = " AND extension='".$extension."'" ;
        $sql = "SELECT id, filename, extension, filesize,introtext,refcode_path,create_date FROM c_app_file WHERE appId=" . $appId . "  AND status=1" . $condition;
        
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        return $rows;
    }
    
    public function getAppFileById($fileId)
    {
        $sql = "SELECT id, appId, filename,extension,refcode_path,create_date FROM c_app_file WHERE id=" . $fileId;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();
        return $rows;   
    }    
    
    public function UpdateDownload($appId, $id)
    {
        $sql = "UPDATE c_app SET download=(download + 1) WHERE id=" . $appId;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->execute();
        
        $sql = "UPDATE c_app_file SET download=(download + 1) WHERE id=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->execute();
        
        return $result;
    }
}  
?>
